package com.smz.service.impl;

import com.smz.dao.TProductHandoverDAO;
import com.smz.entity.PageEntity;
import com.smz.entity.TProductHandover;
import com.smz.service.TProductHandoverService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TProductHandoverServiceImpl implements TProductHandoverService {
    @Autowired
    private TProductHandoverDAO tProductHandoverDAO;


    @Override
    public PageEntity<TProductHandover> findPageProHandover(@Param("start") int start,
                                                            @Param("length") int length,
                                                            @Param("orderId") String orderId,
                                                            @Param("time") String time) {
        int count = Math.toIntExact(tProductHandoverDAO.selectCount(null));
        System.out.println(count);

//
        int pageCount = (count) % (start*length) == 0 ? (count) / (start*length) : (count) % (start*length) +1;
        if (start-1<0){
            start=0;
        }
        List<TProductHandover> tProductHandoverPageEntity = tProductHandoverDAO.selectPageProHandover((start-1)*length,length,orderId,time);

        return new PageEntity<>(tProductHandoverPageEntity,start,tProductHandoverPageEntity.size(),length);
    }

    @Override
    public void AddProHandover(TProductHandover tProductHandover) {
        tProductHandoverDAO.insertProHandover(tProductHandover);
    }

    @Override
    public void removeProHandoverById(Integer id) {
        tProductHandoverDAO.deleteProHandoverById(id);
    }

    @Override
    public void modifyProHandoverById(TProductHandover tProductHandover) {
        tProductHandoverDAO.updateProHandoverById(tProductHandover);
    }

    @Override
    public void modifyProOrderState(Integer id) {
        tProductHandoverDAO.updateProOrderState(id);
    }

    @Override
    public TProductHandover findProHandoverByOrderId(Integer orderId) {
        return tProductHandoverDAO.selectProHandoverByOrderId(orderId);
    }
}
